import vue from '@vitejs/plugin-vue'
import path from 'path'

import { defineConfig, loadEnv } from 'vite'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'

const port = parseInt(process.env.port) || parseInt(process.env.npm_config_port) || 80

const CONFIG_URI = {
	api: 'http://192.168.1.7'
}

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
	const env = loadEnv(mode, process.cwd())

	return {
		resolve: {
			alias: {
				'@': path.resolve(__dirname, './src')
			}
		},

		plugins: [
			vue(),

			createSvgIconsPlugin({
				// 指定要缓存的图标文件夹
				iconDirs: [path.resolve(process.cwd(), 'src/assets/svgs')],

				// 执行 icon name 的格式
				symbolId: 'icon-[name]'
			})
		],

		server: {
			host: '0.0.0.0', // 默认为localhost
			port, // 端口号
			open: false, // 是否自动打开浏览器
			proxy: {
				// 本地开发环境通过代理实现跨域，生产环境使用 nginx 转发
				'/dev-api': {
					target: CONFIG_URI.api, // 后端服务实际地址
					changeOrigin: true,
					//rewrite: (path) => path.replace(/^\/dev-api/, ""),
					rewrite: (path) => path.replace(new RegExp('^' + env.VITE_APP_BASE_API), '')
				}
			}
		}
	}
})
